package achi.community.irla.beans;

import achi.community.irla.database.Crypto;
import achi.community.irla.database.DatabaseImpl;
import achi.community.irla.database.ModelBean;
import achi.community.irla.entities.Achievements;
import achi.community.irla.entities.Users;
import com.icesoft.faces.context.effects.Effect;
import com.icesoft.faces.context.effects.Highlight;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.Serializable;
import java.lang.String;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.application.ViewHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.event.FacesEvent;
import javax.faces.event.ValueChangeEvent;
import javax.faces.validator.ValidatorException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.icefaces.component.fileentry.FileEntry;
import org.icefaces.component.fileentry.FileEntryEvent;
import org.icefaces.component.fileentry.FileEntryResults;

/**
 * RegControlBean:
 * Validation for register.xhtml
 * Adds the new Customer to DB and ModelBean
 */
@ManagedBean
@RequestScoped
public class AddAchiControlBean implements Serializable {

    @EJB
    DatabaseImpl db;
    
    @ManagedProperty(value = "#{addAchiBackBean}")
    AddAchiBackBean backingBean;
    
    private String image = "none";

    public AddAchiBackBean getBackingBean() {
        return backingBean;
    }

    public void setBackingBean(AddAchiBackBean backingBean) {
        this.backingBean = backingBean;
    }

    public void putMessage(String id, String key) {
        ResourceBundle resourceBundle = ResourceBundle.getBundle("MessageBoundle", FacesContext.getCurrentInstance().getViewRoot().getLocale());
        String value = resourceBundle.getString(key);
        FacesMessage msg = new FacesMessage(value);
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(id, msg);
    }

    public String getClientId(String id) {
        FacesContext context = FacesContext.getCurrentInstance();
        UIViewRoot root = context.getViewRoot();
        return findComponent(root, id).getClientId();
    }

    /**
     * Finds component with the given id
     */
    private UIComponent findComponent(UIComponent c, String id) {
        if (id.equals(c.getId())) {
            return c;
        }
        Iterator<UIComponent> kids = c.getFacetsAndChildren();
        while (kids.hasNext()) {
            UIComponent found = findComponent(kids.next(), id);
            if (found != null) {
                return found;
            }
        }
        return null;
    }

    public String buttonClicked() {//ActionEvent evt) {
        System.out.println("clicked!");
        String title_sv = backingBean.getTitle();
        String desc_sv = backingBean.getDescription();
        String category = backingBean.getCategory();
        String sub_category = backingBean.getSubcategory();
        String date_created = new Date().toString();
        String img = image;

        Achievements achi = new Achievements(title_sv, null, desc_sv, null, category, sub_category, img, 0);
        achi.setDate(date_created);
        db.addAchievement(achi);

        return "database";
    }

    public void update() {
        FacesContext context = FacesContext.getCurrentInstance();
        String viewId = context.getViewRoot().getViewId();
        ViewHandler handler = context.getApplication().getViewHandler();
        UIViewRoot root = handler.createView(context, viewId);
        root.setViewId(viewId);
        context.setViewRoot(root);
    }
    
    public void uploadListener(FileEntryEvent e){
        FileEntry fe = (FileEntry)e.getComponent();
        FileEntryResults results = fe.getResults();
        
        File parent = null;
        
        FileEntryResults.FileInfo i = results.getFiles().get(0);
        
        if(i.isSaved()){
            System.out.println("Saved!!!");
            image = i.getFileName();
        }
        else{
            System.out.println("not Saved");
        }
    }
}
